<template>
  <div class="Tables">
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column label="#" type="index" show-overflow-tooltip width="50" align="center"></el-table-column>
      <el-table-column prop="title" label="优惠券名称" width="" align="center">
        <template slot-scope='scope'>
          <span>{{scope.row.title}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="category" label="优惠分类" width="" align="center">
        <template slot-scope='scope'>
          <span>{{scope.row.category.title}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="优惠类型" width="" align="center">
        <template slot-scope='scope'>
          <span>{{scope.row.type.title}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="coupon_money" label="优惠金额" width="100" align="center">
        <template slot-scope='scope'>
          <span>{{scope.row.coupon_money}}元</span>
        </template>
      </el-table-column>
      <el-table-column prop="picture" label="优惠总数量" width="100" align="center">
        <template slot-scope='scope'>
          <span>{{scope.row.coupon_count}}张</span>
        </template>
      </el-table-column>
      <el-table-column prop="coupon_rule" label="订单价大于" width="100" align="center">
        <template slot-scope='scope'>
          <span>{{scope.row.coupon_rule}}元</span>
        </template>
      </el-table-column>
      <el-table-column prop="coupon_limit" label="每人限领" width="100" align="center">
        <template slot-scope='scope'>
          <span>{{scope.row.coupon_limit}}张</span>
        </template>
      </el-table-column>
      <el-table-column prop="coupon_given" label="已发放数量" width="100" align="center">
        <template slot-scope='scope'>
          <span>{{scope.row.coupon_given}}张</span>
        </template>
      </el-table-column>
      <el-table-column prop="coupon_start" label="开始时间" show-overflow-tooltip width="" align="center"></el-table-column>
      <el-table-column prop="coupon_end" label="结束时间" show-overflow-tooltip width="" align="center"></el-table-column>
      <el-table-column prop="validity_term" label="有效时间" show-overflow-tooltip width="" align="center">
        <template slot-scope='scope'>
          <span>领取后{{scope.row.validity_term}}天内有效</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" fixed="right"  width="100" align="center">
        <template slot-scope='scope'>
          <span :class="scope.row.status.class">{{scope.row.status.title}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="is_show" label="首页展示" fixed="right"   align="center">
        <template slot-scope='scope'>
          <el-switch v-model="scope.row.is_show" :disabled="!checkPermission(['couponShow'])" @change="changeshows(scope.row.is_show,scope.row.id)" :active-value="1"
            :inactive-value="0">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作" align="center" v-if="checkPermission(['couponEdit','couponDelete','couponOpen'])"  fixed="right" width="180px">
        <template slot-scope="scope">
          <!-- 需要切换的状态：状态：0-未发布（默认），1-发布中，2-未开始，3-已结束，4-已暂停，5-已抢光 -->
         <!-- 未发布：编辑、发布、删除 0
          未开始：编辑、删除  2
          已结束：编辑、删除 3
          已暂停：发布、结束、删除 4
          已抢光：编辑、删除  success-ok 5
          发布中：暂停、结束  underway 1  -->
          <span class="edittext"  @click="editdialog(scope.row.id)" v-if="scope.row.status.value!=1 && checkPermission(['couponEdit'])">编辑</span>
          <span class="edittext" @click="stopclick('暂停',scope.row.id,4)" v-if="scope.row.status.value==1 && checkPermission(['couponEdit'])">暂停</span>
          <span class="edittext" @click="endclick('结束',scope.row.id,3)" v-if="(scope.row.status.value==1||scope.row.status.value==4) && checkPermission(['couponEdit'])">结束</span>
          <span class="edittext" @click="publishclick('发布',scope.row.id,1)" v-if="(scope.row.status.value==0||scope.row.status.value==4 ||scope.row.status.value==3) && checkPermission(['couponOpen'])">发布</span>
          <span class="edittext" @click="deleteitem(scope.row.id)" v-if="(scope.row.status.value!=1&& scope.row.status.value!=4) && checkPermission(['couponDelete'])">删除</span>
        </template>
      </el-table-column>
    </el-table>
    <el-row>
      <el-col :span="24" class="pagination">
        <!-- 翻页 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pages.page"
          :page-sizes="[10, 20, 30]" background :page-size="10" layout="total, sizes, prev, pager, next" :total="pages.total">
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  //api
  import {changeState,changeshow} from '@/api/coupon'
  //按钮权限验证
  import checkPermission from "@/utils/permission";
  export default {
    data() {
      return {
        //表格数据
        tableData: [],
        //页码数据
        pages: {},
      }
    },
    props: {
      //表格数据
      tabData: {
        type: Object,
        default: () => {
          return {}
        },
      },
    },
    methods: {
      //按钮权限验证
      checkPermission,
      //提示
      hint(title,id,state){
        this.$confirm(`您确认${title}吗？`, '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(async () => {
                    let params = {
                      id,
                      status:state
                    }
                    let data = await changeState(params)
                    if(data.code='0000'){
                      //刷新列表
                      this.$emit('refresh')
                    }
                }).catch(() => {

                });
      },
      //结束
      endclick(title,id,state){
        this.hint(title,id,state)
      },
      //暂停
      stopclick(title,id,state){
        this.hint(title,id,state)
      },
      //发布
      publishclick(title,id,state){
        this.hint(title,id,state)
      },
      //编辑
      editdialog(id){
        this.$emit('openedit',id)
      },
      //删除
      deleteitem(id) {
        this.$emit('deleteitem',id)
      },
      //切换首页展示
      async changeshows(val, id) {
        let state = val ? 0 : 1
        let params = {
          isShow: state,
          id
        }
        let data = await changeshow(params)
        if (data.code == '0000') {
          this.$message.success('更新展示成功！')
          this.$emit('refresh')
        } else {
          this.$emit('refresh')
        }
      },
      //翻页总数
      handleSizeChange(val) {
        this.$emit('changepageTotal', val)
      },
      //翻页
      handleCurrentChange(val) {
        this.$emit('changepage', val)
      },
    },
    watch: {
      tabData(val) {
        this.tableData = val.list
        this.pages = val.page
      },
    },
    mounted() {
      this.tableData = this.tabData.list
      this.pages = this.tabData.page
    }
  }
</script>

<style scoped lang='scss'>
  .Tables {
    margin-top: 25px;
  }

  .edittext {
    margin: 0 5px;
    color: #4c68ef;
    cursor: pointer;
  }

  .pagination {
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
  }
</style>
